<?php

namespace app\admin\controller;

use think\Db;

class System extends Kingsvn {

    // <editor-fold defaultstate="collapsed" desc="管理员菜单管理 -- by hda">
    //菜单列表页面
    public function admin_menu() {
        // $list = db('admin_menu')->order('tree asc')->select();
        $list = db('admin_menu')->order('sort asc')->select();
        $list_tree = $this->tree_asc($list);
        $this->assign('list', $list_tree);
        return $this->fetch();
    }

    // <editor-fold defaultstate="collapsed" desc="管理菜单递归升序排列">


    protected function tree_asc($list, $p_id = 0) {
        $list_tree = array();
        foreach ($list as $menu) {
            if ($menu['p_id'] == $p_id) {

                if (!in_array($menu, $list_tree)) {
                    array_push($list_tree, $menu);
                }
                $list_son = $this->tree_asc($list, $menu['id']);
                if ($list_son) {
                    foreach ($list_son as $menu_son) {
                        array_push($list_tree, $menu_son);
                    }
                }
            }
        }
        return $list_tree;
    }

    // </editor-fold>
    //新意菜单页面
    public function admin_menu_add() {
        $list_icon = db('admin_menu_icon')->select();
        $this->assign('list_icon', $list_icon);

        $list_menu = $this->get_admin_menu_tree_root();
        $this->assign('list_menu', json_encode($list_menu));

        return $this->fetch();
    }

    //新意菜单后台事件
    public function admin_menu_insert() {
        Db::startTrans();

        $data['p_id'] = input('p_id');
        $data['tree'] = $this->get_admin_menu_ids($data['p_id']);
        $data['name'] = input('name');
        $data['icon'] = input('icon');
        $data['status'] = input('status');
        $data['url'] = input('url');
        $data['depth'] = $this->get_admin_menu_depth($data['p_id']);
        $data['sort'] = input('check_last') ? $this->get_admin_menu_sort($data['p_id']) : input('sort');

        $id = $a = db('admin_menu')->insertGetId($data);

        $data2['tree'] = $data['tree'] . "{$id},";

        $b = db('admin_menu')->where('id', $id)->update($data2);

        if ($a > 0 && $b !== false) {
            Db::commit();
            $return['flag'] = true;
            $return['msg'] = '增加成功';
            $return['url'] = url('system/admin_menu');
        } else {
            Db::rollback();
            $return['flag'] = false;
            $return['msg'] = $b;
        }
        die(json_encode($return));
    }

    //修改管理菜单
    public function admin_menu_edit() {
        $list_icon = db('admin_menu_icon')->select();
        $this->assign('list_icon', $list_icon);

        $list_menu = $this->get_admin_menu_tree_root();
        $this->assign('list_menu', json_encode($list_menu));

        $id = input('id');
        $m = db('admin_menu')->where('id', $id)->find();
        $this->assign('m', $m);

        return $this->fetch();
    }

    //修改管理菜单后台事件
    public function admin_menu_update() {
        $where['id'] = $id = input('id');

        $data['p_id'] = input('p_id');
        $data['tree'] = $this->get_admin_menu_ids($data['p_id']) . "{$id},";
        $data['name'] = input('name');
        $data['icon'] = input('icon');
        $data['status'] = input('status');
        $data['url'] = input('url');
        $data['depth'] = $this->get_admin_menu_depth($data['p_id']);
        $data['sort'] = input('check_last') ? $this->get_admin_menu_sort($data['p_id']) : input('sort');

        $a = db('admin_menu')->where($where)->update($data);

        if ($a > 0) {
            $return['flag'] = true;
            $return['msg'] = '修改成功';
            $return['url'] = url('system/admin_menu');
        } else {
            $return['flag'] = false;
            $return['msg'] = $a;
        }
        die(json_encode($return));
    }

    //快速修改管理菜单
    public function admin_menu_update_quick() {
        $where['id'] = input('id');
        $data['name'] = input('name');
        $data['url'] = input('url');
        $data['sort'] = input('sort');
        $data['status'] = input('status') ? 1 : 0;
        $a = db('admin_menu')->where($where)->update($data);
        if ($a !== false) {
            $return['flag'] = true;
            //$return['msg'] = '更新成功';
        } else {
            $return['flag'] = false;
            $return['msg'] = $a;
        }
        die(json_encode($return));
    }

    //删除管理菜单
    public function admin_menu_delete() {
        $id = $where['id'] = input('id');
        $son = db('admin_menu')->where('p_id', $id)->count('id');
        if ($son > 0) {
            $return['flag'] = false;
            $return['msg'] = '该菜单下有子菜单不能删除';
            die(json_encode($return));
        }
        $a = db('admin_menu')->where($where)->delete();
        if ($a !== false) {
            $return['flag'] = true;
            $return['msg'] = '已删除';
            $return['data'] = $id;
        } else {
            $return['flag'] = false;
            $return['msg'] = $a;
        }
        die(json_encode($return));
    }

// <editor-fold defaultstate="collapsed" desc="管理菜单树">

    protected function get_admin_menu_tree_root($root_name = '根菜单') {
        $array_tree = array();
        $array['id'] = '0';
        $array['text'] = $root_name;
        $array['icon'] = false;
        $array['state'] = array('opened' => false);
        $array['children'] = array();

        $list = db('admin_menu')->order('sort asc')->select();
        $array['children'] = $this->get_admin_menu_tree($list);

        array_push($array_tree, $array);
        return $array_tree;
    }

    protected function get_admin_menu_tree($list, $pid = 0) {
        $list1 = array();
        foreach ($list as $m) {
            if ($m['p_id'] == $pid) {
                array_push($list1, $m);
            }
        }

//        $list = db('admin_menu')->where(array('p_id' => $pid))->order('sort asc')->select();

        $array_tree = array();
        foreach ($list1 as $m) {
            $array = array();
            $array['id'] = $m['id'];
            if ($pid > 0) {
                $array['parent'] = $m['p_id'];
            }
            $array['text'] = $m['name'];
            $array['icon'] = false;
            //$array['icon'] = 'jstree-file';
            $array['state'] = array('opened' => false);

            $array_son = $this->get_admin_menu_tree($list, $m['id']);
            if (count($array_son) > 0) {
                $array['children'] = array();
                $array['children'] = $array_son;
            }
            array_push($array_tree, $array);
        }

        return $array_tree;
    }

// </editor-fold>
// 
// <editor-fold defaultstate="collapsed" desc="递归管理菜单取父id">
    protected function get_admin_menu_ids($id) {
        $ids = $id . ',';
        if ($id > 0) {
            $menu = db('admin_menu')->where(array('id' => $id))->find();
            $pid = $menu['p_id'];
            if ($pid > 0) {
                $ids = $this->get_admin_menu_ids($pid) . $ids;
            } else {
                $ids = $pid . ',' . $ids;
            }
        }
        return $ids;
    }

// </editor-fold>
// 
// <editor-fold defaultstate="collapsed" desc="获取菜单当前深度">
    protected function get_admin_menu_depth($id) {
        $depth = 0;
        $menu = db('admin_menu')->where(array('id' => $id))->find();
        if ($menu) {
            $depth = intval($menu['depth']) + 1;
        }
        return $depth;
    }

// </editor-fold>
// 
// <editor-fold defaultstate="collapsed" desc="获取菜单同级最后的排序">
    protected function get_admin_menu_sort($id) {
        $sort = 0;
        $menu = db('admin_menu')->where(array('p_id' => $id))->order('sort desc')->limit(1)->find();
        if ($menu) {
            $sort = intval($menu['sort']) + 1;
        }
        return $sort;
    }

// </editor-fold>
// 
    // </editor-fold>
    // 
    // 
    // <editor-fold defaultstate="collapsed" desc="管理员权限管理">
    // <editor-fold defaultstate="collapsed" desc="管理员">
    //管理员列表
    public function admin_user() {
        
    }

    //新增管理员
    public function admin_user_add() {
        
    }

    //修改管理员
    public function admin_user_edit() {
        
    }

    //删除管理员
    public function admin_user_delete() {
        
    }

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="管理角色">
    //管理员角色列表
    public function admin_role() {
        return $this->fetch();
    }

    //新增管理角色
    public function admin_role_add() {
        $list_menu = $this->get_admin_menu_tree_root('所有权限');
        $this->assign('list_menu', json_encode($list_menu));
        return $this->fetch();
    }

    //新意菜单后台事件
    public function admin_role_insert() {

        $power = input('power');
        $power_array = explode(',', $power);
        $data['power'] = in_array(0, $power_array) ? '0' : $power; //超级权限为0值
        $data['name'] = input('name');
        $data['status'] = input('status');
        $data['sort'] = input('sort');


        $t = db('admin_role')->where(['name' => $data['name']])->find();
        if (!$t) {
            $a = db('admin_role')->insertGetId($data);

            if ($a > 0) {
                $return['flag'] = true;
                $return['msg'] = '增加成功';
                $return['url'] = url('system/admin_role');
            } else {
                $return['flag'] = false;
                $return['msg'] = 'DB ERROR';
            }
        } else {
            $return['flag'] = false;
            $return['msg'] = '角色名称已存在';
        }
        die(json_encode($return));
    }

    //修改管理角色
    public function admin_role_edit() {
        
    }

    //删除管理角色
    public function admin_role_delete() {
        
    }

    // </editor-fold>
// </editor-fold>
}
